package jdk8.lambda;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class FunctionDemo {

	public static void main(String[] args) {
		//
		Function<String, Integer> fsi = new Function<String, Integer>() {
			
			@Override
			public Integer apply(String t) {
				//返回字符串的长度
				return t.length();
			}
		};
		//单独调用
		System.out.println(fsi.apply("jack"));
		//
		List<String> names = Arrays.asList("jack","ann","peter","jackson");
		//
		System.out.println("------ 使用Function完成转换----------");
		names.stream().map(fsi).forEach( (t) -> System.out.println(t));
		System.out.println("----------------------------");
		names.stream().map( (t) -> t.length() ).forEach( (e) -> System.out.println(e));
		
		System.out.println("===================================================");
		Function<String, LocalDate> fsl = (t) -> LocalDate.parse(t);
		//
		LocalDate time = fsl.apply("2009-12-13");
		System.out.println(time);
		
		System.out.println("===== andThan方法使用 [以前面一个Function的输出做为后一个Function的输入======");
		Function<String, Integer> year = fsl.andThen( (t) -> t.getYear());
		System.out.println(year.apply("2003-09-18"));
		
	}
}
